package com.controller;

import com.config.MyWebSocketHandler;
import com.pojo.UserMessage;
import com.pojoVO.CommentInfoVo;
import com.service.UserMessageService;
import com.util.ServerResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.socket.TextMessage;

import java.util.List;

/**
 * Created by Administrator on 2020/3/20.
 */
@Api(tags = "用户消息推送API")
@Controller
public class UserMessageController {
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    public MyWebSocketHandler handler;
    @Autowired
    private UserMessageService userMessageService;
    @ApiOperation(value ="添加用户消息",notes ="添加用户消息" )
    @PostMapping("/userMessage")
    @ResponseBody
    public ServerResponse add(UserMessage userMessage){
        // ServerResponse serverResponse =
        //System.out.println("serverResponse:"+serverResponse);
        return userMessageService.save(userMessage);
    }

    @ApiOperation(value = "根据消息的id删除消息",notes ="")
    @DeleteMapping("/userMessage/{id}")
    @ResponseBody
    public ServerResponse del(@PathVariable("id")String id){
        ServerResponse serverResponse = userMessageService.modifyStatus(Integer.valueOf(id));
        System.out.println("serverResponse:"+serverResponse);
        return serverResponse;
    }

    @ApiOperation(value ="修改消息",notes ="" )
    @PutMapping("/userMessage")
    @ResponseBody
    public ServerResponse upd(UserMessage userMessage){
        System.out.println(userMessage);
        ServerResponse serverResponse = userMessageService.modify(userMessage);
        return serverResponse;
    }

    @ApiOperation(value ="根据消息id查询消息")
    @GetMapping("/userMessage/{id}")
    @ResponseBody
    public ServerResponse findById(@PathVariable("id") String id){
        System.out.println(id);
        ServerResponse serverResponse = userMessageService.getById(Integer.valueOf(id));
        return serverResponse;
    }

    @ApiOperation(value ="发送消息")
    @GetMapping("/send/{id}")
    @ResponseBody
    public ServerResponse send (@PathVariable("id") String id){
        System.out.println(id);
        amqpTemplate.convertAndSend("topicExchange","systemInfo",id);
        return ServerResponse.addSuccess();
    }
    @RabbitListener(queues = "systemInfo")
    public void Recive(CommentInfoVo msg) throws Exception{
        List<String> idlist = handler.sendMsgToFront(new TextMessage("用户名为"+msg.getReplyUserName()+"回复了你"+msg.getCommentContext()), msg);
        for (String id:idlist
             ) {
            System.out.println("发送消息给客户端id="+id);
        }

    }



    @ApiOperation(value ="查询所有消息")
    @GetMapping("/userMessage/backhome")
    public String findAll(Model model){
        ServerResponse serverResponse = userMessageService.getAll();
        model.addAttribute("info",serverResponse.getData());
        return "back/user/userMessage";
    }

}
